var index;
// 初始化
$(function () {
	var username=localStorage.getItem("username");
	$("#addUser").val(username);
	var projectId = getProjectUrlParam("projectId", "project");
	$("#projectId").val(projectId);
	$("[name=files]").live('change', setFileUrl);
	// 初始化层
	divShow();
//	$("[name=keyValue]").live('onblur', setKeyValue);
//  没有修改功能，现在只是添加功能，所以没有初始化信息	
//	getProcess();
});
//添加列表行
function addRow(key, value) {
	if(key == undefined || key == null){
		key = "";
	}
	if(value == undefined || value == null){
		value = "";
	}
	// 表示现在到第几行
	index = $('#myform [name=value]').size();
	$('.g_tabName').append("" +
		"<tr id='keyValue"+index+"Tr'>" +
			"<td>" +
				"<input name='keyValue' id='keyValue"+index+"' value='"+key+"' onblur='setKeyValue("+index+")' maxlength='30' linkCheckSlave"+index+"/> " +
			"</td>" +
			"<td class='g_chuanJus'>" +
				"<input name='value' id='value"+index+"' value='"+value+"' style='float:left;'/>"+
				"<a href='###' class='l_addfile' style='position: relative;float: left;width:26px;height: 18px;margin-top:2px;border: 1px solid #ededed;text-align: center;line-height: 18px;display: inline-block;zoom: 1;font-size: 13px;color: #1777d8;'>"+
					"<input needCheck linkCheckMaster='"+index+"' fileType=' .jpg .jpeg .bmp .png ' type='file' id='file"+index+"' name='files' style='position: absolute;left:0;top:0;width:26px;height: 18px;appearance: none; filter:alpha(opacity:0);opacity: 0;border: 1px solid #EDEDED;z-index: 1;'>上传</input>"+
		    	"</a>"+
				"<a href='javascript:imgShow("+index+");' style='margin-left:2px;'>预览</a>"+
				"<a href='javascript:delRow("+index+");'>删除</a>"+
				"<input name='key' id='key"+index+"' type='hidden' value='"+key+"' maxlength='100'/>" +
			"</td>" +
			"<td>"+
				"<input name='processDesc' id='processDesc'"+index+" />"+
			"</td>"+
		"</tr>"
	);
}
//删除列表行
function delRow(index) {
	$("#keyValue"+index+"Tr").remove();
}
//添加列表行隐藏值
function setKeyValue(index) {
	var keyValue = $('#keyValue'+index).val();
	$('#key'+index).val(keyValue);
}
//选定上传文件
function setFileUrl() {
	var id = $(this).attr("id");
	var index = id.replace("file","");
	$('#value'+index).val($(this).val());
}
//查询项目及其负责人信息
function getProcess() {
	var projectId = getProjectUrlParam("projectId");
	$("#projectId").val(projectId);
	$.ajax({
		url : server_url+'/rest/projectProcess/list.do?token='+token+'&ran='+Math.random(),
		type : 'post',
		dataType : 'json',
		data : {'projectId':projectId},
		success : function(data) {
			if (data.retCode = "0000") {
				// 表示现在到第几行
				var indexTr = $('#myform [name=value]').size();
				var processList = data.data;
				$.each(processList,function(index,vo){
					// 如果有固定行数的可以直接添加信息,直到在原有的基础上添加完后，在添加新的行
					if(indexTr > index){
						$("#key"+index).val(vo.processName);
						$("#keyValue"+index).val(vo.processName);
						$("#value"+index).val(vo.processUrl);
						$("#processDesc"+index).val(vo.processDesc);
					}else{
						addRow(vo.processName, vo.processUrl);
					}
					/*// 如果有固定信息的可以直接添加信息
					if("项目负责人" == vo.postName){
						$("#staffName0").val(vo.staffName);
					}else{
						addPostName(vo.postName, vo.staffName);
					}*/
				});
				
			}else{
				easyDialog.open({
					container:{
						header:"温馨提示",
						content:data.retMsg
					},
					noFn : true							
				});
			}
		}
	});
}
//添加
function add() {
	if(checkForm('myform')){
		return;
	}
	var data=$('#myform').serializeArray();
	var dataobj={
	};
	$.each(data, function(i, field){
		if("key" == field.name || "value" == field.name || "processDesc" == field.name){
 			if(dataobj[field.name] instanceof Array){
 				dataobj[field.name].push(field.value);
 			}else{
 				dataobj[field.name] = [field.value];
 			}
 		}else{
 			dataobj[field.name] = field.value;
 		}
	});
	var uplist = $("input[name=files]");  
    var arrId = [];  
    for (var i=0; i< uplist.length; i++){  
        if(uplist[i].value){  
            arrId[i] = uplist[i].id;  
        }  
    }  
	$.ajaxFileUpload({
		url : server_url+'/rest/projectProcess/add.do?token='+token+'&ran='+Math.random(),
		type : 'post',
		secureuri : false,  
		fileElementId:arrId,
		data : dataobj,
		success : function(data) {
			if (data.retCode = "0000") {
				var url = "projectList.do?ran="+Math.random();
				tl_config.loadToWorkArea(url);
			}else{
				easyDialog.open({
					container:{
						header:"温馨提示",
						content:data.retMsg
					},
					noFn : true							
				});
			}
		},
		error: function(e) { 
			console.log(e);
		} 
	});
}
//预览图片
function imgShow(index) {
	var file = $("#file"+index).get(0).files[0];
//	$('#showImg').src = URL.createObjectURL(file);
	var img = new Image(), url = img.src = URL.createObjectURL(file);
    var $img = $(img);
    img.onload = function() {
        URL.revokeObjectURL(url);
        $('#showDiv').empty().append($img);
    };
    $(".z_graybg,.l_showimg").show();
}

//显示层
function divShow() {
	$(".z_graybg,.l_showimg").hide();
	$(".z_graybg").height($(document).height());
	var L_pic=($(window).width()-$("#showDiv img").width())/2;
	var T_pic=($(window).height()-$("#showDiv img").height())/2;
	$("#showDiv").css({"left":L_pic,"top":T_pic});
	$(window).resize(function(){
		var l_pic=($(window).width()-$("#showDiv img").width())/2;
		var t_pic=($(window).height()-$("#showDiv img").height())/2;
		$("#showDiv").css({"left":l_pic,"top":t_pic});
	});
	$("#showDiv").click(function(){
		$(".z_graybg,.l_showimg").hide();
	});
}

